//
//  DeviceLockViewController.m
//  ERP
//
//  Created by QA on 15/9/12.
//  Copyright (c) 2015年 ShangHai HaoLei Software Technology Co., Ltd. All rights reserved.
//

#import "DeviceLockViewController.h"
#import "PasswordViewController.h"
#import "CLLockVC.h"
#import "CoreArchive.h"
#import "FingerprintLock.h"
#import "DeviceLockIntroduceViewController.h"

#import "ExitLoginService.h"

@interface DeviceLockViewController ()<UITableViewDelegate>

@property (nonatomic,strong) NSArray *listInfoArray;
@property (nonatomic,strong) NSArray *listExplainArray;

@end

@implementation DeviceLockViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [self setTitle: @"安全中心"];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.tableView.backgroundColor = ThemeColorBackgroundGary;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self setTableViewShowCell];
}

- (void)setTableViewShowCell
{
    //判断是否手势开启密码锁
    BOOL hasPwd = [CLLockVC hasPwd];
    if (hasPwd) {
        self.listInfoArray = @[@[@"开启手势密码锁定", @"重置手势密码"],
                               @[@"开启Touch ID指纹解锁"],
                               @[@"登录密码修改"] ];
        
        self.listExplainArray = @[@"开启手势密码锁定，可以有效保护账户安全",
                                  @"开启后，可使用Touch ID解锁汇基云",
                                  @""];
    } else {
        self.listInfoArray = @[@[@"开启手势密码锁定"],
                               @[@"登录密码修改"] ];
        
        self.listExplainArray = @[@"开启手势密码锁定，可以有效保护账户安全",
                                  @""];
    }
    
    //页面刷新
    [self.tableView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - 数据源方法
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.listInfoArray.count;
}

#pragma mark 返回每组行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSArray *array = [self.listInfoArray objectAtIndex:section];
    return array.count;
}

#pragma mark返回每行的单元格
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    
    NSArray *array = [self.listInfoArray objectAtIndex:indexPath.section];
    cell.textLabel.text= [array objectAtIndex:indexPath.row];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    if (indexPath.section == 0 && indexPath.row == 0) { //手势密码
        UISwitch *switchView = [[UISwitch alloc] initWithFrame:CGRectZero];
        [switchView setOn:[CLLockVC hasPwd] animated:NO];
        [switchView addTarget:self action:@selector(gestureSwitchChanged:) forControlEvents:UIControlEventValueChanged];
        
        cell.accessoryView = switchView;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.accessoryType = UITableViewCellAccessoryNone;
        
    } else if (indexPath.section == 1 && indexPath.row == 0 && [CLLockVC hasPwd]){ //指纹密码
        UISwitch *switchView = [[UISwitch alloc] initWithFrame:CGRectZero];
        BOOL isPasscodeSet = [FingerprintLock isPasscodeSet];
        [switchView setOn:isPasscodeSet animated:NO];
        [switchView addTarget:self action:@selector(fingerprintSwitchChanged:) forControlEvents:UIControlEventValueChanged];
        
        cell.accessoryView = switchView;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 22;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 30;
}

#pragma mark 返回每组尾部说明
-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
    NSString *explainString = [self.listExplainArray objectAtIndex:section];
    return explainString;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if ((indexPath.section == 2 && indexPath.row == 0) ||
        (indexPath.section == 1 && indexPath.row == 0 && ![CLLockVC hasPwd])) { //登录密码修改
        PasswordViewController *viewController = [[PasswordViewController alloc] init];
        [self.navigationController pushViewController:viewController animated:YES];
        
    } else if (indexPath.section == 0 && indexPath.row == 1) { //重置手势密码
        [CLLockVC showModifyLockVCInVC:self.navigationController successBlock:^(CLLockVC *lockVC, NSString *pwd) {
            NSLog(@"密码重置成功");
            [lockVC dismiss:.5f];
        }];
    }
}

//手势密码开启监听
- (void)gestureSwitchChanged:(id)sender
{
    BOOL hasPwd = [CLLockVC hasPwd];
    if (!hasPwd) {
        DeviceLockIntroduceViewController *viewController = [[DeviceLockIntroduceViewController alloc] init];
        [self.navigationController pushViewController:viewController animated:YES];
    } else {
        [CLLockVC showVerifyLockVCInVC:self.navigationController forgetPwdBlock:^{
            NSLog(@"忘记密码");
            [ExitLoginService exitAppUserLogin];
            
        } successBlock:^(CLLockVC *lockVC, NSString *pwd) {
            NSLog(@"密码正确");
            //移除密码
            [CLLockVC colsePwd];
            [FingerprintLock removePasscode];
            [lockVC dismiss:0.f];
        }];
    }
}

//指纹密码锁开启监听
- (void)fingerprintSwitchChanged:(id)sender
{
    UISwitch *switchView = sender;
    BOOL isPasscodeSet = [FingerprintLock isPasscodeSet];
    
    if (!isPasscodeSet) {
        BOOL isOpenSuccess = [FingerprintLock openPasscode];
        if(!isOpenSuccess) [switchView setOn:NO animated:YES];
    } else {
        //关闭指纹解锁验证
        [FingerprintLock showPasscodeCompletion:^(BOOL success, NSError *error) {
            if (success) [FingerprintLock removePasscode];
            else [switchView setOn:YES animated:YES];
        } localizedReason:@"通过验证指纹关闭Touch ID指纹解锁"];
    }
    
}

@end
